import baseurl from './config.js' //公共接口地址

const userInfo = uni.getStorageSync('token'); //获取token
const token = userInfo.token //获取token

// 请求封装
function request(obj) {
	let url = baseurl + obj.url; //地址
	let data = obj.data || {};
	let success = obj.success;
	let method = obj.method || 'POST';
	uni.request({
		url: url,
		data: data,
		method: method,
		header: {
			'token': token,
			//'Authorization': token,
		},
		success: function(res) {
			// 判断token是否过期
			if (res.data.code == 401) {
				uni.showModal({
					title: '提示',
					content: res.data.message,
					showCancel: false,
					success: function(res) {
						if (res.confirm) {
							uni.redirectTo({
								url: '@/pages/register/register'
							})
						}
					}
				})
				return
			}
			
			success(res)
			
		},
		complete: function(res) {
			console.log("请求成功")
		},
	})
}

export default {
	request: request
};
